#include <jni.h>
#include <string>

#include "code/AndroidLog.h"

#include "code/Decodec.h"
//#include "CallBackToJava.h"

JavaVM *javaVM = NULL;
CallBackToJava *callJava = NULL;
Decodec *ffmpeg;
PlayStatus *playStatus;

bool isExit = true;
pthread_t threadStart;


//通过JNI_OnLoad  初始化JavaVM
extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *resversed) {
    jint result = -1;
    JNIEnv *env;
    javaVM = vm;
    if (vm->GetEnv((void **) (&env), JNI_VERSION_1_6) != JNI_OK) {
        return result;
    }
    return JNI_VERSION_1_6;
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_stringFromJNI(JNIEnv *env, jobject thiz) {
    // TODO: implement stringFromJNI()
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}extern "C"
JNIEXPORT void JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1parpared(JNIEnv *env, jobject thiz,
                                                           jstring source) {

    const char *videoSource = env->GetStringUTFChars(source, 0);
    if (NULL == ffmpeg) {
        if (NULL == callJava) {
            callJava = new CallBackToJava(javaVM, env, &thiz);
        }
        //native 回调开始加载
        LOGE("native load start");
        callJava->onCallLoad(MAIN_THREAD, true);
        playStatus = new PlayStatus();
        ffmpeg = new Decodec(callJava, videoSource, playStatus);
        ffmpeg->onParpared();
    }
}


void *startCallBack(void *data) {
    Decodec *decodec = static_cast<Decodec *>(data);
    decodec->start();
//    pthread_exit(&threadStart);
    return 0;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1start(JNIEnv *env, jobject thiz) {
    if (NULL != ffmpeg) {
//        ffmpeg->start();
        pthread_create(&threadStart, NULL, startCallBack, ffmpeg);
    }
}extern "C"
JNIEXPORT void JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1pause(JNIEnv *env, jobject thiz) {
    // TODO: implement n_pause()
    if(NULL!=ffmpeg){
        ffmpeg->pause();
    }
}extern "C"
JNIEXPORT void JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1resume(JNIEnv *env, jobject thiz) {
    // TODO: implement n_resume()
    if(NULL!=ffmpeg){
        ffmpeg->resume();
    }
}extern "C"
JNIEXPORT void JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1stop(JNIEnv *env, jobject thiz) {
    // TODO: implement n_stop()
    if(!isExit){
        return;
    }

    if(NULL!=ffmpeg){
        ffmpeg->release();
        pthread_join(threadStart,NULL);//阻塞，等待线程退出

        delete(ffmpeg);
        ffmpeg=NULL;
        if(NULL!=callJava){
            delete(callJava);
            callJava=NULL;
        }
        if(NULL!=playStatus){
            delete(playStatus);
            playStatus=NULL;
        }
    }
    isExit= true;
}extern "C"
JNIEXPORT jint JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1duration(JNIEnv *env, jobject thiz) {
    // TODO: implement n_duration()
    if(NULL!=ffmpeg){
        return ffmpeg->videoDuration;
    }
    return 0;
}extern "C"
JNIEXPORT void JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1setVolume(JNIEnv *env, jobject thiz,
                                                            jint percent) {
    if(NULL!=ffmpeg){
        ffmpeg->setVolume(percent);
    }
}extern "C"
JNIEXPORT void JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1seek(JNIEnv *env, jobject thiz,
                                                       jint seek_position) {
    if(NULL!=ffmpeg){
        ffmpeg->seek(seek_position);
    }
}extern "C"
JNIEXPORT void JNICALL
Java_com_renfei_ffmpegvideoplayer_FFmpegPlayer_n_1setmute(JNIEnv *env, jobject thiz, jint mute) {

    if(NULL!=ffmpeg){
        ffmpeg->setMute(mute);
    }
}